#include "app_lis2dh12.h"
#include "x_nucleo_iks01a1.h"

/*private function*/
static APP_LIS2DH12_StatusTypeDef app_lis2dh12_check_id(void);
/*private variable*/
float sensitivity = 0.0;

int tmp1[3] = {0, 0, 0};
APP_LIS2DH12_StatusTypeDef app_lis2dh12_init(void)
{
     /*init io*/
     if(LIS2DH12_Init() != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     /*read id*/
     if(app_lis2dh12_check_id() != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     
     /*get sensitivity*/
     if(LIS2DH12_Get_Sensitivity(&sensitivity) != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     
     /*power mode set*/
     if(LIS2DH12_Power_Mode_Set(LIS2DH12_LOW_POWER_MODE) != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
      
     /*ODR set*/
     if(LIS2DH12_Data_Rate_Set(LIS2DH12_ODR_LP_1620HZ) != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     
     /*full scale set*/
     if(LIS2DH12_FS_Set(LIS2DH12_FS_2G) != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     
     /*fifo enable*/
     if(LIS2DH12_FIFO_Enable(LIS2DH12_FIFO_ENABLE)!= APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     
     /*fifo mode set*/
     if(LIS2DH12_FIFO_Mode_Set(LIS2DH12_FIFO_MODE_STREAM) != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;   
    
     /*fifo fth int set*/
     if(LIS2DH12_FIFO_WTM_INT1_Enable(LIS2DH12_WTM_INT1_ENABLE)!= APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     
     /*fifo fth value set*/
     if(LIS2DH12_FIFO_FTH_Value_Set(LIS2DH12_SAMPLE_NUMBER - 1)!= APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
      
      /*x , y, z enable*/
     if(LIS2DH12_Axis_Enable(LIS2DH12_AXIS_X_ENABLE | LIS2DH12_AXIS_Y_ENABLE | LIS2DH12_AXIS_Z_ENABLE)!= APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     
     if(LIS2DH12_FIFO_Buffer_Reset()!= APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;

     if(LIS2DH12_INT_IO_ITConfig() != APP_LIS2DH12_OK)
     return APP_LIS2DH12_ERROR;
     
     return APP_LIS2DH12_OK;
}

static APP_LIS2DH12_StatusTypeDef app_lis2dh12_check_id(void)
{
     uint8_t id;
     if(LIS2DH12_Read_Id(&id) != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
     
     if(id != I_AM_LIS2DH12)
         return APP_LIS2DH12_ERROR;
     
     return APP_LIS2DH12_OK;
}

APP_LIS2DH12_StatusTypeDef app_lis2dh12_get_3_axis_value(float* data)
{
    
    if(LIS2DH12_X_Y_Z_Get_Axes_Data(data) != APP_LIS2DH12_OK)
         return APP_LIS2DH12_ERROR;
    
    return APP_LIS2DH12_OK;
}
